package com.zxj.camerademo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class FaceView extends View {

    private Paint mPaint;
    private String mColor = "#42ed45";
    private List<RectF> mFaces = new ArrayList<>();//人脸信息

    public FaceView(Context context) {
        this(context,null);
        init(context);
    }

    public FaceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
        init(context);
    }

    public FaceView(Context context,@Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor(mColor));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, context.getResources().getDisplayMetrics()));
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (RectF face : mFaces) {//因为会同时存在多张人脸，所以用循环
            canvas.drawRect(face, mPaint);//绘制人脸所在位置的矩形
        }
    }

    //设置人脸信息，然后刷新FaceView
    public void setFaces(List<RectF> faces){
        this.mFaces = faces;
        invalidate();
    }
}
